﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading.Tasks;

namespace Util.Helper
{
    public static class FileHelper
    {
        /// <summary>
        /// 创建压缩文件
        /// </summary>
        /// <param name="path">文件夹全路径</param>
        /// <returns></returns>
        public static async Task<Stream> CreateZipArchive(string path)
        {
            MemoryStream returnStream = new MemoryStream();
            MemoryStream compressStream = new MemoryStream();
            using var zip = new ZipArchive(compressStream, ZipArchiveMode.Create);
            DirectoryInfo di = new DirectoryInfo(path);
            await AddToZipArchive(di, zip);
            compressStream.Position = 0;
            await compressStream.CopyToAsync(returnStream);
            returnStream.Position = 0;
            return returnStream;
        }

        /// <summary>
        /// 级联把文件加入压缩档
        /// </summary>
        /// <param name="di">文件夹对象</param>
        /// <param name="zip">压缩档对象</param>
        /// <returns></returns>
        private static async Task AddToZipArchive(DirectoryInfo di, ZipArchive zip)
        {
            string dirName = di.Name;
            ZipArchiveEntry zipEntry = zip.CreateEntry(dirName, CompressionLevel.NoCompression);
            using var entryStream = zipEntry.Open();
            var dis = di.GetDirectories();
            foreach(var d in dis)
            {
                await AddToZipArchive(d, zip);
            }
            var files = di.GetFiles();
            foreach (var file in files)
            {
                using var zipToStream = file.OpenRead();
                await zipToStream.CopyToAsync(entryStream);
            }
        }

        /// <summary>
        /// 删除对应的目录（包含其子目录及文件）
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteDirectory(string path)
        {
            if (System.IO.Directory.Exists(path))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                //删除子文件
                foreach (var item in dirInfo.GetFiles())
                {
                    File.Delete(item.FullName);
                }

                //删除子目录
                foreach (var item in dirInfo.GetDirectories())
                {
                    DeleteDirectory(item.FullName);
                }

                //删除当前目录
                Directory.Delete(path);
            }
        }

        /// <summary>
        /// 壓縮文件
        /// </summary>
        /// <param name="streams">文件流字典</param>
        /// <returns></returns>
        //public static Stream CompressFiles(Dictionary<string, Stream> streams)
        //{
        //    byte[] buffer = new byte[6500];
        //    MemoryStream returnStream = new MemoryStream();
        //    MemoryStream compressStream = new MemoryStream();
        //    using (ZipOutputStream zipStream = new ZipOutputStream(compressStream))
        //    {
        //        zipStream.SetLevel(9);
        //        foreach (var item in streams)
        //        {
        //            string filename = item.Key;
        //            using (var streamInput = item.Value)
        //            {
        //                ZipEntry ze = new ZipEntry(filename);
        //                ze.IsUnicodeText = true;
        //                zipStream.PutNextEntry(ze);
        //                while (true)
        //                {
        //                    var readCount = streamInput.Read(buffer, 0, buffer.Length);
        //                    if (readCount > 0)
        //                    {
        //                        zipStream.Write(buffer, 0, readCount);
        //                    }
        //                    else
        //                    {
        //                        break;
        //                    }
        //                }
        //                zipStream.Flush();
        //            }
        //        }
        //        zipStream.Finish();
        //        compressStream.Position = 0;
        //        compressStream.CopyTo(returnStream);//加上這名是因為using后 compressStream已關閉，所以在沒有關閉之前把數據Copy到另一個對象上進行返回
        //    }
        //    returnStream.Position = 0;
        //    return returnStream;
        //}
    }
}
